<?php

Class Dbcon{
	
	public $location;
	public $user;
	public $password;
	public $my_db;
	public $mysqli;
	public $result;
	
	function __construct() {
		$this->location = '127.0.0.1';
		$this->user =  'root';
		$this->password = '';
		$this->my_db = 'test';
		/*$this->location = 'churchmacau.db.11043964.hostedresource.com';
		$this->user =  'churchmacau';
		$this->password = 'A11bb22cc33@';
		$this->my_db = 'churchmacau';*/
	}
	
	public function getSelect($sql)
	{
		 $this->mysqli = new mysqli($this->location, $this->user, $this->password, $this->my_db);
		 
		 $this->result = $this->mysqli->query($sql);
		 try{
		 	if ($this->result) { 
			  if($this->result->num_rows>0){
			  	$resultSet = array();
				while($row =$this->result->fetch_array() ){
					$resultSet[] = $row;
				}  
				$this->result->free();
			  	return $resultSet;
			  }
			}
			return "No Data";
		 }catch(Exception $e)
		 {
		 	$this->result->free();
		 	return "Error";
		 }
	}
	
	public function setInsert($sql)
	{
		$this->mysqli = new mysqli($this->location, $this->user, $this->password, $this->my_db);
		 
		 try{
		 	$this->result = $this->mysqli->query($sql);
		 	$arows = $this->mysqli->insert_id;
			return $arows;
		 }catch(Exception $e)
		 {
		 	return "Error";
		 }
	}
	
	public function setUpdate($sql)
	{
		$this->mysqli = new mysqli($this->location, $this->user, $this->password, $this->my_db);
		 
		 try{
		 	$this->result = $this->mysqli->prepare($sql);
			$this->result->execute();
			return true;
		 }catch(Exception $e)
		 {
		 	return "Error";
		 }
	}
	
	public function clear()
	{
		if (isset($this->mysqli))
		  {
		  	$this->mysqli->close();
			$this->mysqli = null;
		  }
	}
}
?>